﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

using MT.eVotePRS.Device;
using MT.eVotePRS.Model;
// using MT.eVotePRS.Service;

using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Logic;

namespace MT.eVotePRS.Presentation.Presenter
{
    public class CheckInPresenter
    {
        private IDeviceAPIHelper arsHelper;

        private IViewCheckIn view;

        private StudentActions studentAction;
        private HandsetActions handsetAction;
        private InventoryTxnActions txnAction;

        public StudentActions StudentAction
        {
            get { return studentAction; }
            set { studentAction = value; }
        }
        public HandsetActions HandsetAction
        {
            get { return handsetAction; }
            set { handsetAction = value; }
        }
        public InventoryTxnActions TxnAction
        {
            get { return txnAction; }
            set { txnAction = value; }
        }

        public void SetView(IViewCheckIn view, IDeviceAPIHelper ars)
        {
            this.view = view;
            view.PropertyChanged += OnViewPropertyChanged;
            view.CheckInClicked += OnCheckInClicked;

            arsHelper = ars;
            arsHelper.DataReceived += OnDataReceived;
            arsHelper.StartProcess();
        }

        void OnDataReceived(object sender, ARSEventArgs e)
        {
            HandsetData handset = (HandsetData)e.ReceivedData;
            switch (handset.CommandCode)
            {
                case ARSCommandCode.STUDENT_JOIN:
                    view.Handset.MAC = handset.MacStr;

                    // TODO: Check Handset Status
                    InventoryTxn txn = txnAction.CheckHandsetStatus(view.Handset.MAC);
                    if (txn != null)
                    {
                        if (txn.Status == Util.Constants.Status_CheckIn.Id)
                        {
                            throw new Exception(string.Format("{0} was Checked In.", txn.HandsetID));
                        }
                        else
                        {
                            view.Txn = txn;
                            StudentInfo student = studentAction.FindStudentByNo(view.Txn.StudentID);
                            view.Student = student;
                            view.EnableCheckIn();
                        }
                    }
                    else
                    {
                        throw new Exception (string.Format("Handset ({0}) can not checked in.", view.Handset.MAC));

                    }

                    break;

                default:
                    break;
            }
        }

        private void OnViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }

        protected virtual void OnCheckInClicked(object sender, EventArgs e)
        {
            SaveTxn();
        }

        void SaveTxn()
        {
            // TODO: Save Student and Check Out transaction
            try
            {
                InventoryTxn inventoryTxn = new InventoryTxn();
                inventoryTxn.HandsetID = view.Handset.MAC;
                inventoryTxn.StudentID = view.Student.StudentNo;
                inventoryTxn.TxnTime = DateTime.Now;
                inventoryTxn.Remark = view.EventRemark;
                inventoryTxn.Status = Util.Constants.Status_CheckIn.Id;
                inventoryTxn.UserEvent = Util.Constants.Status_CheckIn.StatusDesc;
                inventoryTxn.NeedPay = false;
                inventoryTxn.PaymentNo = string.Empty;
                inventoryTxn.IsOverride = false;
                inventoryTxn.OverrideStatus = -1;

                object txnID = txnAction.CreateNewTxn(inventoryTxn);
                if (txnID != null && Convert.ToInt32(txnID) > 1)
                {
                    view.Handset = new Handsets();
                    view.Student = new StudentInfo();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
